﻿using System; 
using System.IO;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.EntryModel;
using NTTDATA.BEENChina.ResultCertification.Result;
using Ntt.NttData.Been.DeliveryCheck.Command;
using Ntt.NttData.Been.DeliveryCheck.DEntry.BeenWCFHelper;

namespace NTTDATA.BEENChina.ResultCertification.Util
{
    public class ImageDownloadHelper
    {
        private string _orderReceiveDate;

        public VerfityJobItem GetImageData(VerfityJobItem item, string orderReceiveDate)
        {

            _orderReceiveDate = orderReceiveDate;

            
                foreach (ChildImage image in item.ChildImage)
                {
                    try{
                        
                        GetImageData(image);
                     }
                     catch (Exception ex){

                         Log.Console.Error(string.Format("ImageDownloadHelper.GetImageData err jobid:{0} imageid:{1}",item.VerfityJobID,image.ImageId));
                         Log.Console.Error("ImageDownloadHelper.GetImageData", ex);
                         
                     }
                }
     

            return item;
        }

        private string _wcfAddress = string.Empty;

        public    string ImageWcfAddress
        {
            get
            {
                if (_wcfAddress.Equals(string.Empty))
                {
                    _wcfAddress = Util.ConnectHelper.GetImageBackupServiceAddress();
                }

                return _wcfAddress;
            }
        }

        private static string ImageFolder
        {
            get
            {
                return ConnectHelper.GetImageFolder();
            }
        }

        public   void GetImageData(ChildImage image)
        {
             
            var imageFileDownLoadAccess = new DataStore.ImageFileDownloadAccess();

            string ipAndSavePaht = imageFileDownLoadAccess.GetImageDownLoadParam(image.OrderId);

            //没得到下载参数
            //找NS文件夹
            //
            if (String.IsNullOrEmpty(ipAndSavePaht))
            {
                GetNSImage(image);

                
            }
            else
            {
                
           
                string childPicParam = string.Empty;

                if (image.ImageId.Contains("-"))
                {
                    childPicParam =
                        Path.Combine(image.ImageId.Substring(image.ImageId.Length - 3), image.ImageId);
                }

                if (!String.IsNullOrEmpty(childPicParam))
                {
                    image.ImageFullPath = Path.Combine(ipAndSavePaht, childPicParam + ".TIF");
                }
                else
                {
                    image.ImageFullPath = ipAndSavePaht;
                    image.ImageFullPath = Path.Combine(image.ImageFullPath, image.ImageId + ".TIF");
                }
        
          
                //add image download address

                if (image.ImageFullPath.Length > 0 && image.ImageFullPath.Contains("_"))
                {
                    string[] splitInfo = image.ImageFullPath.Split(new char[] {'_'});
                    string imageFilePath = splitInfo[1];

                    image.ImageDownLoadAddress = string.Format(ImageWcfAddress, splitInfo[0]);
                    image.ImageDownLoadParam = imageFilePath;


                    try
                    {
                        WCFHelper.Url = image.ImageDownLoadAddress;
                        ImageInformation imageData = WCFHelper.GetInstance().TRunFileDownLoad("1", image.ImageDownLoadParam);

                        //本次工作结束 清空 否则出错
                        WCFHelper.GetInstance().ClearWcf();

                        if (imageData != null)
                        {
                            image.ImageData = imageData.Image;
                            image.ImageExtension = imageData.ImageExtension;
                        }
                    }
                    catch (Exception ex)
                    {
                    
                        throw ex;
                    }
               
                }
                else
                {
                
            
                    GetNSImage(image);
                }
            }
           
        }

        private void GetNSImage(ChildImage image)
        {
            //Ns画像位置
            string imageDir = ImageFolder;

            if (!String.IsNullOrEmpty(_orderReceiveDate))
            {
                //Ns文件夹 + orderdate(joblist 画面上时间)
                imageDir = Path.Combine(imageDir, _orderReceiveDate);
            }


            string[] files = null;

            if (imageDir != null && Directory.Exists(imageDir))
            {
                files = Directory.GetFiles(imageDir, image.ImageId + "*", SearchOption.AllDirectories);
            }


            if (files != null && files.Any())
            {
                image.ImageFullPath = System.IO.Path.GetFileName(files[0]);
                image.ImageData = File.ReadAllBytes(files[0]);
            }
        }
    }
}
